package app

import (
	"html/template"
	"net/http"

	"github.com/gin-gonic/gin"
	"lucidity.note/app/handler"
	"lucidity.note/ui"
)

type option func(*gin.Engine)

var (
	options = []option{}
	engine  *gin.Engine
)

func init() {
	engine = gin.New()
	gin.SetMode(gin.DebugMode)
	engine.Use(gin.Recovery())
}

func Router() *gin.Engine {
	// 初始化静态资源点
	engine.SetHTMLTemplate(template.Must(template.New("").ParseFS(ui.Template, "template/page/**")))
	engine.StaticFS("/assets", http.FS(ui.Asset))
	engine.StaticFS("/ui", http.FS(ui.Vue))

	// 注册请求路由
	register()
	return engine
}

func register() {
	// 添加页面路由
	options = append(options, func(engine *gin.Engine) {
		engine.GET("/app", func(ctx *gin.Context) { ctx.HTML(http.StatusOK, "app.html", nil) })
		engine.GET("/page/test", func(ctx *gin.Context) { ctx.HTML(http.StatusOK, "test.html", nil) })
	})

	// 添加数据请求路由
	options = append(options, handler.RegisterNoteBookRouter)
	options = append(options, handler.RegisterNoteTreeRouter)
	options = append(options, handler.RegisterRemoteSyncRouter)
	options = append(options, handler.RegisterUserSettingRouter)

	// 注册路由
	for _, router := range options {
		router(engine)
	}
}
